using System;
using System.Globalization;
using System.Threading;
using System.Web;
using Framework.Library.Utilities;

namespace Framework.Library
{
	public class HitCounterHttpModule: IHttpModule
	{
		private HttpApplication _application;
		public void Init(HttpApplication app)
		{
			this._application = app;
			this._application.BeginRequest += new EventHandler(_application_BeginRequest);
		}
		public void _application_BeginRequest(object sender, EventArgs e)
		{

            //string url = HttpContext.Current.Request.Url.AbsolutePath;
          
            //if (url.Contains("404.aspx"))
            //{
            //    string[] urlInfo404 = HttpContext.Current.Request.Url.Query.ToString().Split(';');

            //    if (urlInfo404.Length > 1)
            //    {
            //        string originalUrl = urlInfo404[1];
            //        string[] urlParts = originalUrl.Split('?');
            //        string requestedFile = string.Empty;

            //        //if (urlParts.Length > 1)
            //        //{
            //        //    requestedFile = urlParts[0];
            //        //}
            //        //else
            //        //{
            //            requestedFile = urlParts[0];
            //        //}
                      
            //        if (requestedFile.IndexOf('.') > 0)
            //        {
            //            // There's some extension, so this is not an extensionless URL.
            //            // Don't handle such URL because these are really missing files
            //        }
            //        else
            //        {
            //            UrlRedirection oPR = new UrlRedirection();

            //            string strURL = requestedFile;
            //            strURL = strURL.Substring(strURL.IndexOf("/portalid"));
            //            string strRewrite = oPR.GetMatchingRewrite(strURL);
                       
            //            if (!String.IsNullOrEmpty(strRewrite))
            //            {
            //                strURL = strRewrite;
            //            }
                       
            //            string langCulture = AppEnv.GetLanguage();
            //            CultureInfo cur = new CultureInfo(langCulture);
            //            Thread.CurrentThread.CurrentCulture = cur;
            //            Thread.CurrentThread.CurrentUICulture = cur;

            //            int count = ConvertUtility.ToInt32(Components.Portal.PortalSetting.GetSetting()[Definition.Portal_Hit_Counter]);
            //            count++;
            //            Components.Portal.PortalSetting.SetValue(Definition.Portal_Hit_Counter, count.ToString());

            //            HttpContext.Current.RewritePath("~" + strURL);

            //        }
            //    }
            //}

		    //using isapi

            string strPath = HttpContext.Current.Request.Url.AbsolutePath;
            if (strPath.IndexOf("Modules") > 0) return;
            if (strPath.IndexOf("Images") > 0) return;

            int count = ConvertUtility.ToInt32(Components.Portal.PortalSetting.GetSetting()[Definition.Portal_Hit_Counter]);
            count++;
            Components.Portal.PortalSetting.SetValue(Definition.Portal_Hit_Counter, count.ToString());
            string langCulture = AppEnv.GetLanguage();
            CultureInfo cur = new CultureInfo(langCulture);
            Thread.CurrentThread.CurrentCulture = cur;
            Thread.CurrentThread.CurrentUICulture = cur;
		}
		public void Dispose()
		{
			
		}
	}
}
